// JavaScript Document


$(document).ready(function() {
    $('.numeric').keydown(function(event) {
                // Allow special chars + arrows 
                if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 
                    || event.keyCode == 27 || event.keyCode == 13 
                    || (event.keyCode == 65 && event.ctrlKey === true) 
                    || (event.keyCode >= 35 && event.keyCode <= 39)){
                        return;
                }else {
                    // If it's not a number stop the keypress
                    if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                        event.preventDefault(); 
                    }   
                }
            });
			
			
		$('.numeric').bind('keypress', function(e) {
	        return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
	    });
});


$(document).ready(function () {
	 
	$( "#add_row" ).click(function() {

		var num_row = $("#hidden_num").val();

		num_row = parseFloat(num_row) + parseFloat(1);
		$("#hidden_num").val(num_row);
		


		$("<div class='row'>minnimum:<input type='text' name='minnimum[]' id='minnimum"+num_row+"' value='' /> ea. &nbsp; price/1 ea:<input type='text' name='price[]' id='price"+num_row+"' value='' /> ฿<input class='del_price' type='button' value='delete' /></div>").insertBefore("#addNewRow");


   });
   
   
   /*$( "#add_row" ).click(function() {

		var num_row = $("#hidden_num").val();
		
		var minnimum = $("#minnimum").val();
		var price = $("#price").val();
		var show_min = $("#show_min").val();
		

		num_row = parseFloat(num_row) + parseFloat(1);
		$("#hidden_num").val(num_row);
		
		
		$("#show_min").val(show_min+"minnimum : "+minnimum+" ea. price/1 ea : "+price+" ฿ , \n");
		


		//$("<span>"+minnimum+" / "+price+"</span><input class='del_price' type='button' value='delete' /></div>").insertBefore("#show_min");


   });*/
   
   
   

	$( "#add_row_color" ).click(function() {
	
		var num_row = $("#hidden_num_color").val();

		num_row = parseFloat(num_row) + parseFloat(1);
		$("#hidden_num_color").val(num_row);
		

		$("<div class='row'>Color name:<input type='text' name='color[]' id='color"+num_row+"' value='' /><input class='del_color' type='button' value='delete' /></div>").insertBefore("#addNewRow_color");
		
   });
   

   

});

$( document ).on( "click", ".del_color", function() {
			
			 $(this).closest('div.row').remove();
		 
			 var num_row = $("#hidden_num_color").val();
			 
			num_row = parseFloat(num_row) - parseFloat(1);
			$("#hidden_num_color").val(num_row);
});




$( document ).on( "click", ".del_price", function() {
			
			 $(this).closest('div.row').remove();
		 
			 var num_row = $("#hidden_num").val();
			 
			num_row = parseFloat(num_row) - parseFloat(1);
			$("#hidden_num").val(num_row);
});



function hide_package(){
	$('option[value=""]').prop('disabled', true); 
	//$("#package option[value='']").remove();
};

function hide_product_format(){
	$('option[value=""]').prop('disabled', true); 
//	$("#product_format option[value='']").remove();
};



function popup(form) {
    window.open('', 'formpopup','width=640,height=1024,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
    form.target = 'formpopup';
}

/*
 |™*******************************************************************************************™*
 | Pull down change image
 |™*******************************************************************************************™*
*/
$(document).ready(function() {
    $("#format option").hover(function() {
        var imgUrl = $(this).val();
        $("#swapImg_format").attr("src", imgUrl);
    });
	
    $("#package option").hover(function() {
        var imgUrl = $(this).val();
        $("#swapImg_package").attr("src", imgUrl);
    });
});
/*
 |™*******************************************************************************************™*
 | Calulate
 |™*******************************************************************************************™*
*/
	$(document).ready(function (){
			// update the plug-in version
			$("#idPluginVersion").text($.Calculation.version);

			// bind the recalc function to the quantity fields
//			$("input[name^=qty_item_]").bind("keyup blur", gettype);
			// run the calculation function now
//			gettype();
			
			
			$("input[name^=qty_item_],#qty_0").bind("keyup change", recalc);
			// run the calculation function now
			recalc();

			// automatically update the "#totalSum" field every time
			// the values are changes via the keyup event
			$("input[name^=sum]").sum("keyup", "#totalSum");
		}
	);
	function gettype(){

		var id = jQuery("#qty_item_1").val();
	//	var code = jQuery("#pro_item_code_0").val();
		jQuery.get("get_type.php?type=" + id , "", function(data) {
 	 		$('#qty_0').html(data);
		});
	}

	function recalc(){
		var qty, price, qty_val, unit_price;
		qty = $("input[name^=qty_item_]");
		price = $("#qty_0");
    	qty_val = qty.val();
		/*if ((qty_val > 0) && (qty_val < 100))
		{
		//	 price = $("input[name^=qty_item_]");
			 price = 20;
		}
		else if (qty_val < 49)
		{
			price = 15;
		}
		else
		{
			price = 30;
		}*/
		
    	$("[id^=price_item_]").text(price * qty_val);
	
		$("[id^=total_item]").calc(
			// the equation to use for the calculation
			"qty * price",
			// define the variables used in the equation, these can be a jQuery object
			{
				qty: qty,
            	price: price,
			},
			// define the formatting callback, the results of the calculation are passed to this function
			function (s){
				// return the number as a dollar amount
				return "￥" + s.toFixed(2);
			},
			// define the finish callback, this runs after the calculation has been complete
			function ($this){
				// sum the total of the $("[id^=total_item]") selector
				var sum = $this.sum();
				
				$("#grandTotal").text(
					// round the results to 2 digits
					"￥" + sum.toFixed(2)
				);
			},
			gettype()
		);
	}

/*
 |™*******************************************************************************************™*
 | End Calulate
 |™*******************************************************************************************™*
*/
$(document).ready(function() {
/*-----------------------------------------------------------------------------------------*/
jQuery("#pro_item_code_0").blur(function() {
		//blockUI();
		var id = jQuery("#pro_item_code_0").val();
		get_type(id);		// ส่งค่าไปเลือกจำนวนสินค้า						  
});
/*-----------------------------------------------------------------------------------------*/
//		 ฟังชันก์รับ-ส่งค่าจำนวนสินค้า
/*-----------------------------------------------------------------------------------------*/
/*function get_type() {
		var id = jQuery("#lengthcolor").val();
		jQuery.get("http://localhost/Project/W/workserve/httpdocs/get_type.php?type=" + id, "", function(data) {
 	 		$('#total_amount').html(data);
		});
}*/
/*-----------------------------------------------------------------------------------------*/
//		 ฟังชันก์ประเภทลูกค้า จำนวนสินค้า
/*-----------------------------------------------------------------------------------------*/

jQuery("#lengthcolor").change(function() {
		//blockUI();
		var id = jQuery("#lengthcolor").val();
		//alert(id);
		get_qty(id);								  
		var id = jQuery("#lengthcolor").val();
		//get_price(id);								  

});
/*function get_qty() {
		var id = jQuery("#lengthcolor").val();
	//	var code = jQuery("#pro_item_code_0").val();
		jQuery.get("get_type.php?type=" + id , "", function(data) {
 	 		$('#total_amount').html(data);
		});
}*/
});
